package com.vaadin.tutorial.crm.ui.view.list;

import com.vaadin.tutorial.crm.backend.entity.Score;
import com.vaadin.tutorial.crm.ui.MainLayout;
import com.vaadin.flow.component.Tag;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.grid.GridVariant;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.value.ValueChangeMode;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.tutorial.crm.backend.entity.Student;
import com.vaadin.tutorial.crm.backend.service.ScoreService;
import com.vaadin.tutorial.crm.backend.service.StudentService;


@Route(value = "score-list", layout = MainLayout.class)
@PageTitle("scoreList")
@Tag("scoreList-view")
public class ScoreView extends VerticalLayout {
    private ScoreService scoreService;
    private ScoreForm scoreForm;

    private Grid<Score> grid = new Grid<>(Score.class);
    private TextField filterText = new TextField();

    public ScoreView(ScoreService scoreService, StudentService studentService) {
        this.scoreService = scoreService;
        configureGrid();

        //表单
        scoreForm = new ScoreForm(studentService.findAll());
        scoreForm.addListener(ScoreForm.SaveEvent.class, this::saveScore);
        scoreForm.addListener(ScoreForm.DeleteEvent.class, this::deleteScore);
        scoreForm.addListener(ScoreForm.CloseEvent.class, e -> closeEditor());

        //将表格、表单添加到一个div
        Div div = new Div(grid, scoreForm);
        div.addClassName("main");
        div.setSizeFull();

        //将toolbar、div添加到界面
        add(getToolbar(), div);
        updateList("");
        closeEditor();
    }
    
    private void configureGrid() {
        grid.addClassName("score-grid");
        grid.addThemeVariants(GridVariant.LUMO_ROW_STRIPES);
        grid.removeAllColumns();
//        grid.setSizeFull();
        grid.addColumn("scoMath").setHeader("高等数学");
        grid.addColumn("scoPhysics").setHeader("大学物理");
        grid.addColumn("scoEnglish").setHeader("大学英语");
        grid.addColumn("scoComputer").setHeader("计算机基础");
        grid.addColumn("scoAlgebra").setHeader("线性代数");
        grid.addColumn("scoCircuit").setHeader("电路");
        grid.addColumn(score -> {
            Student student = score.getStudent();
            return student == null ? "-" : student.getStuName();
        }).setHeader("学生姓名");
        grid.getColumns().forEach(col -> col.setAutoWidth(true));

        //点击表格中的某一列添加到表单中
        grid.asSingleSelect().addValueChangeListener(event ->
                editScore(event.getValue()));
    }

    //搜索框和添加按钮作为一个toolbar
    private HorizontalLayout getToolbar() {
        filterText.setPlaceholder("请输入学号或姓名...");
        filterText.setClearButtonVisible(true);
        filterText.setValueChangeMode(ValueChangeMode.LAZY);
        filterText.addValueChangeListener(e -> updateList(e.getValue()));

        Button addScoreButton = new Button("新增成绩信息");
        addScoreButton.addClickListener(click -> addScore());

        HorizontalLayout toolbar = new HorizontalLayout(filterText, addScoreButton);
        toolbar.addClassName("toolbar");
        return toolbar;
    }
    
    //更新表格
    private void updateList(String value) {
        grid.setItems(scoreService.findAll(value));
    }

    //清除表单的值
    private void closeEditor() {
        scoreForm.setScore(null);
        scoreForm.setVisible(false);
        removeClassName("editing");
    }

    public void editScore(Score score) {
        if (score == null) {
            closeEditor();
        } else {
            scoreForm.setScore(score);
            scoreForm.setVisible(true);
            addClassName("editing");
        }
    }

    //保存
    private void saveScore(ScoreForm.SaveEvent event) {
        scoreService.save(event.getScore());
        updateList("");
        closeEditor();
    }

    //删除
    private void deleteScore(ScoreForm.DeleteEvent event) {
        scoreService.delete(event.getScore());
        updateList("");
        closeEditor();
    }

    void addScore() {
        grid.asSingleSelect().clear();
        editScore(new Score());
    }
}
